package com.zbkj.crmeb.store.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crmeb.core.exception.CrmebException;
import com.zbkj.crmeb.enums.HomeStyleEnum;
import com.zbkj.crmeb.enums.HomeStyleTemplateEnum;
import com.zbkj.crmeb.store.dao.HomeStyleDao;
import com.zbkj.crmeb.store.dao.HomeStyleTemplateDao;
import com.zbkj.crmeb.store.dto.HomeStyleDTO;
import com.zbkj.crmeb.store.dto.HomeStyleTemplateDTO;
import com.zbkj.crmeb.store.model.HomeStyle;
import com.zbkj.crmeb.store.model.HomeStyleTemplate;
import com.zbkj.crmeb.store.service.HomeStyleTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class HomeStyleTemplateServiceImpl extends ServiceImpl<HomeStyleTemplateDao, HomeStyleTemplate> implements HomeStyleTemplateService {
    @Resource
    private HomeStyleTemplateDao homeStyleTemplateDao;


    @Override
    public List<HomeStyleTemplateDTO> listEntity() {
        List<HomeStyleTemplate> list = this.list(Wrappers.<HomeStyleTemplate>lambdaQuery()
                .isNull(HomeStyleTemplate::getActivityId)
                .groupBy(HomeStyleTemplate::getTemplateId)
                .orderByAsc(HomeStyleTemplate::getTemplateId)
        );
        // 对象复制
        List<HomeStyleTemplateDTO> homeStyleTemplateDTOs = new ArrayList<>();
        for (HomeStyleTemplate homeStyleTemplate : list) {
            HomeStyleTemplateDTO dto = new HomeStyleTemplateDTO();
            BeanUtils.copyProperties(homeStyleTemplate, dto);
            dto.setId(null);
            dto.setSort(null);
            dto.setTemplateId(homeStyleTemplate.getTemplateId().getValue());
            dto.setTemplateName(homeStyleTemplate.getTemplateId().getMsg());
            homeStyleTemplateDTOs.add(dto);
        }

        return homeStyleTemplateDTOs;
    }

    @Override
    public List<HomeStyleTemplateDTO> listEntityByType(String templateId) {
        List<HomeStyleTemplate> list = this.list(Wrappers.<HomeStyleTemplate>lambdaQuery()
                .eq(HomeStyleTemplate::getTemplateId, templateId)
                .isNull(HomeStyleTemplate::getActivityId)
                .orderByAsc(HomeStyleTemplate::getSort)
        );
        // 对象复制
        List<HomeStyleTemplateDTO> homeStyleTemplateDTOs = new ArrayList<>();
        for (HomeStyleTemplate homeStyleTemplate : list) {
            HomeStyleTemplateDTO dto = new HomeStyleTemplateDTO();
            BeanUtils.copyProperties(homeStyleTemplate, dto);
            dto.setId(null);
            dto.setTemplateId(homeStyleTemplate.getTemplateId().getValue());
            dto.setType(String.valueOf(homeStyleTemplate.getType().getValue()));
            dto.setPosition(JSON.parseObject(homeStyleTemplate.getPosition()));
            dto.setParame(JSON.parseObject(homeStyleTemplate.getParame()));
            dto.setStyle(JSON.parseObject(homeStyleTemplate.getStyle()));
            homeStyleTemplateDTOs.add(dto);
        }

        return homeStyleTemplateDTOs;
    }

    @Override
    public Boolean saveEntity(List<HomeStyleTemplateDTO> homeStyleTemplateDTOS) {
        List<HomeStyleTemplate> homeStyleTemplates = new ArrayList<>();
        for (int i = 0; i < homeStyleTemplateDTOS.size(); i++) {
            if (null == homeStyleTemplateDTOS.get(i).getTemplateId()) {
                throw new CrmebException("模板不能为空");
            }
            if (null == homeStyleTemplateDTOS.get(i).getType()) {
                throw new CrmebException("类型不能为空");
            }
            // 对象赋值
//            homeStyleTemplateDTOS.get(i).setMerId(merId);
            HomeStyleTemplate homeStyleTemplate = new HomeStyleTemplate();
            BeanUtils.copyProperties(homeStyleTemplateDTOS.get(i), homeStyleTemplate);
            homeStyleTemplate.setSort(i);
            homeStyleTemplate.setTemplateId(HomeStyleTemplateEnum.valueOf(homeStyleTemplateDTOS.get(i).getTemplateId()));
            homeStyleTemplate.setType(HomeStyleEnum.valueOf(homeStyleTemplateDTOS.get(i).getType()));
            homeStyleTemplate.setPosition(homeStyleTemplateDTOS.get(i).getPosition().toJSONString());
            homeStyleTemplate.setParame(homeStyleTemplateDTOS.get(i).getParame().toJSONString());
            homeStyleTemplate.setStyle(homeStyleTemplateDTOS.get(i).getStyle().toJSONString());
            if (null == homeStyleTemplate.getId()) {
                homeStyleTemplate.setCreateTime(new Date());
            } else {
                HomeStyleTemplate homeStyleTemplateDB = this.getOne(Wrappers.<HomeStyleTemplate>lambdaQuery()
                        .eq(HomeStyleTemplate::getTemplateId, homeStyleTemplateDTOS.get(i).getTemplateId())
                        .eq(HomeStyleTemplate::getId, homeStyleTemplateDTOS.get(i).getId())
                        .last("LIMIT 1")
                );
                if (null == homeStyleTemplateDB) {
                    throw new CrmebException("记录不存在");
                }
            }
            homeStyleTemplate.setUpdateTime(new Date());
            homeStyleTemplates.add(homeStyleTemplate);
        }

        return this.saveOrUpdateBatch(homeStyleTemplates);
    }


}
